local limu = fk.CreateSkill{
  name = "hanqing__limu",
  tags = { Skill.Compulsory },
  dynamic_desc = function (self, player, lang)
    return "hanqing__limu_inner:"..player:getMark(self.name)
  end,
}

Fk:loadTranslationTable{
  ["hanqing__limu"] = "立木",
  [":hanqing__limu"] = "锁定技，当一名角色造成伤害后，若本局所有角色造成伤害值之和达到{1}点，其与你依次摸一张牌直到以此法摸牌数达到{1}张，"..
  "然后你清空本局伤害记录并令{}里的数字+1。",

  [":hanqing__limu_inner"] = "锁定技，当一名角色造成伤害后，若本局所有角色造成伤害值之和达到{1}点，其与你依次摸一张牌直到以此法摸牌数达到{1}张，"..
  "然后你清空本局伤害记录并令{}里的数字+1。",

  ["@hanqing__limu_record"] = "立木",
}

limu:addEffect(fk.Damage, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(limu.name) and target and
      player:getMark("@hanqing__limu_record") >= player:getMark(limu.name)
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    if not target.dead then
      targets = {target}
    end
    table.insertIfNeed(targets, player)
    local total, n = player:getMark(limu.name), 0
    local to = targets[1]
    while n < total do
      n = n + 1
      to:drawCards(1, limu.name)
      local loop_lock = 0
      while true do
        to = to:getNextAlive()
        if table.contains(targets, to) then
          break
        end
        loop_lock = loop_lock + 1
        if loop_lock > 20 then
          return
        end
      end
    end
    if not player.dead then
      room:setPlayerMark(player, "@hanqing__limu_record", 0)
      if player:hasSkill(limu.name, true) then
        room:addPlayerMark(player, limu.name, 1)
      end
    end
  end,

  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(limu.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "@hanqing__limu_record", data.damage)
  end,
})

limu:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  room:setPlayerMark(player, limu.name, 1)
  if not is_start then
    local n = 0
    room.logic:getActualDamageEvents(1, function (e)
      if e.data.from then
        n = n + e.data.damage
      end
    end, Player.HistoryGame)
    room:setPlayerMark(player, "@hanqing__limu_record", n)
  end
end)

limu:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@hanqing__limu_record", 0)
end)

return limu
